﻿//
//

var gadget = new WeatherGadget();
var weather;
var index = -1;
var timer;

function init() {
    checkDockState(); // check the initial state

    // setup the event handlers for docking later
    System.Gadget.onDock = dockGadget;
    System.Gadget.onUndock = undockGadget;
    System.Gadget.onSettingsClosed = settingsClosed;

    // if using a flyout function, set the

    gadget.onDataReady = updateInformer;
    gadget.performRequest();
    gadget.beginPoll();
}

function updateInformer(forecast) {
    weather = forecast;
    index = -1;
    if (timer != undefined) {
        clearTimeout(timer);
    }
    onTimer();
}

function getSign(temperature) {
    if (temperature > 0) {
        return '+';
    }
    return "";
}

function translatePictName(pict) {
    var name = pict.split(".")[0];

    if (name == "_255_NA") {
        return "";
    }

    return name + ".png";
}

function translateWind(wind) {
    var m = [
        { range: 20, text: "С" },
        { range: 35, text: "С,С-В" },
        { range: 55, text: "С-В" },
        { range: 70, text: "В,С-В" },
        { range: 110, text: "В" },
        { range: 125, text: "В,Ю-В" },
        { range: 145, text: "Ю-В" },
        { range: 160, text: "Ю,Ю-В" },
        { range: 200, text: "Ю" },
        { range: 215, text: "Ю,Ю-З" },
        { range: 235, text: "Ю-З" },
        { range: 250, text: "З,Ю-З" },
        { range: 290, text: "З" },
        { range: 305, text: "З,С-З" },
        { range: 325, text: "С-З" },
        { range: 340, text: "С,С-З" },
        { range: 360, text: "С" }
    ];

    var course = 0;

    for (var index = 0; index < m.length; index++) {
        var item = m[index];

        if (course <= wind && wind <= item.range) {
            return item.text;
        }
        course = item.range;
    }
    return "";
}

function translateDay(day, hour) {
    var diff = day - new Date().getDate();

    if (diff > 0) {
        if (diff != 1 || hour != 3) {
            return "завтра";
        }
    }

    return "сегодня";
}

function updateForecast(forecast) {
    var tod = "";

    with (forecast.temperature) {
        var s1 = getSign(min);
        var s2 = getSign(max);

        switch (forecast.hour) {
            case 3:
                tod = "ночью";
                break;

            case 9:
                tod = "утром";
                break;

            case 15:
                tod = "днем";
                break;

            case 21:
                tod = "вечером";
                break;
        }
    }

    day.innerText = translateDay(forecast.date.getDate(), forecast.hour) + ' ' + tod;
    weatherLogo.src = "../images/" + translatePictName(forecast.pict);
    with (forecast.temperature) {
        temperature.innerText = s1 + min + ".." + (s2 != s1 ? s2 : "") + max + '°';
    }
    pressure.innerText = forecast.pressure.min + ".." + forecast.pressure.max + " мм";
}

function onTimer() {
    var temp = weather;

    if (System.Gadget.visible) {
        if (temp) {
            city.innerText = temp.city.name;
            city.title = temp.city.region + ", " + temp.city.country;

            if (index == -1) {
                if (temp.locator) {
                    var current = temp.locator;
                    var sign = getSign(current.temperature.value);
                    var today = "сейчас";

                    if (temp.lastUpdated > temp.locator.expires) {
                        today += " (примерно)";
                    }

                    day.innerText = today;
                    weatherLogo.src = "../images/" + translatePictName(current.pict);
                    temperature.innerText = sign + current.temperature.value + '°';
                    with (current.temperature) {
                        temperature.title = "по ощущению " + getSign(feel) + feel + '°';
                    }
                    pressure.innerText = current.pressure + " мм";
                    wind.innerText = translateWind(current.wind.course) + ", " + current.wind.speed + " м/с";
                }
                else {
                    index = nextIndex(index, temp.forecast);

                    if (index > -1) {
                        updateForecast(temp.forecast[index]);
                    }
                }
            }
            else {
                updateForecast(temp.forecast[index]);
            }

            index = nextIndex(index, temp.forecast);
        }
    }
    timer = setTimeout(onTimer, 20000);
}

function nextIndex(index, list) {
    if (++index >= list.length) {
        index = -1;
    }
    return index;
}

function dockGadget() {
    // TODO: add your docking functions here
    var oBody = document.body.style;

    //showOrHide("docked", true);
    //showOrHide("undocked", false);
}

function undockGadget() {
    // TODO: add your undocking functions here
    var oBody = document.body.style;

    //showOrHide("undocked", true);
    //showOrHide("docked", false);
}

function settingsClosed(event) {
    if (event.closeAction == event.Action.commit) {
        gadget.reset();
    }
}

function flyout() {
    System.Gadget.Flyout.file = "flyout.html";
    System.Gadget.Flyout.show = true;
}

function checkDockState() {
    if (System.Gadget.docked) {
        dockGadget();
    }
    else {
        undockGadget();
    }
}

function showOrHide(oHTMLElement, bShowOrHide) {
    try {
        if (typeof (oHTMLElement) == "string") {
            oHTMLElement = document.getElementById(oHTMLElement);
        }
        if (oHTMLElement && oHTMLElement.style) {
            if (bShowOrHide == 'inherit') {
                oHTMLElement.style.visibility = 'inherit';
            }
            else {
                if (bShowOrHide) {
                    oHTMLElement.style.visibility = 'visible';
                }
                else {
                    oHTMLElement.style.visibility = 'hidden';
                }
                try {
                    if (bShowOrHide) {
                        oHTMLElement.style.display = 'block';
                    }
                    else {
                        oHTMLElement.style.display = 'none';
                    }
                }
                catch (ex) {
                }
            }
        }
    }
    catch (ex) {
    }
}